//
//  YKTopicPictureView.m
//  baisi
//
//  Created by 吴高烽 on 16/10/8.
//  Copyright © 2016年 吴高烽. All rights reserved.
//  如果view的frame已经明确设置了，但没有按照想想的位置发生了变化，就要考虑autoresizing的问题

#import "YKTopicPictureView.h"
#import "YKTopic.h"
#import <UIImageView+WebCache.h>
#import "YKProgressView.h"
#import "YKShowPictureViewController.h"
@interface YKTopicPictureView ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIImageView  *gifView;
@property (weak, nonatomic) IBOutlet UIButton *seeBigButton;
@property (weak, nonatomic) IBOutlet YKProgressView *progressView;

@end

@implementation YKTopicPictureView
+(instancetype)pictureView{
    return  [[NSBundle mainBundle]loadNibNamed:NSStringFromClass(self) owner:nil options:nil][0];
}
-(void)awakeFromNib{
    [super awakeFromNib];
    self.autoresizingMask = UIViewAutoresizingNone ;

    self.imageView.userInteractionEnabled = YES ;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(showPicture)]];
}
- (void)showPicture{
    YKShowPictureViewController *pictureVC = [[YKShowPictureViewController alloc]init];
    pictureVC.topic = self.topic ;
    [YKKeyWindow.rootViewController presentViewController:pictureVC animated:YES completion:nil];
}
-(void)setTopic:(YKTopic *)topic{
    _topic = topic ;
    [self.progressView setProgress: topic.pictureProgress animated:YES];
   // YKTopic *topic = []
    [self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.large_image] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        self.progressView.hidden = NO;
        topic.pictureProgress = 1.0*receivedSize/expectedSize ;
        [self.progressView setProgress:topic.pictureProgress animated:YES];
//        self.progressView.progressLabel.text = [NSString stringWithFormat:@"%.0f%%",progress*100];

    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        self.progressView.hidden = YES;
        if (topic.isBigPicture == NO ) return  ;
        UIGraphicsBeginImageContextWithOptions(topic.pictureVF.size, YES, 0.0);
        // 将下载网的image对象绘制到图形上下文中
        CGFloat width = topic.pictureVF.size.width ;
        CGFloat height = width * image.size.height / image.size.width ;
        [image drawInRect:CGRectMake(0, 0, width, height)];
        self.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
    }];
    //[self.imageView sd_setImageWithURL:[NSURL URLWithString:topic.large_image]];
    if (topic.is_gif) {
        self.gifView.hidden = NO;
    }else{
        self.gifView.hidden = YES;
    }
    if (topic.isBigPicture) {
        self.seeBigButton.hidden = NO ;
    }else{
        self.seeBigButton.hidden = YES ;
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
/**ImageIO框架 将gif对象解析n个image对象，用imageView 的animation来播放*/
@end
